package com.attendance.www.hr.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.RequestAuthorityBean;
import com.attendance.www.hr.beans.RequestAuthorityPageBean;
import com.attendance.www.hr.dao.mapper.RequestAuthorityExtMapper;
import com.attendance.www.hr.dao.mapper.RequestAuthorityMapper;
import com.attendance.www.hr.entitys.RequestAuthority;
import com.attendance.www.hr.entitys.RequestAuthorityExample;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IRequestAuthorityService;

@Service
public class RequestAuthorityServiceImpl implements IRequestAuthorityService {

	@Resource
	private RequestAuthorityMapper requestAuthorityDao;
	@Resource
	private RequestAuthorityExtMapper requestAuthorityExtDao;
	
	
	/**
	 * 新增产假
	 */
	@Override
	public boolean saveRequestAuthority(RequestAuthorityBean requestAuthorityBean) {
		int employeeId=requestAuthorityBean.getEmployeeId();
		int requestType=requestAuthorityBean.getRequestType();
		//新增验证，是否已添加过
		RequestAuthorityExample requestAuthorityExample=new RequestAuthorityExample();
		requestAuthorityExample.createCriteria().andEmployeeIdEqualTo(employeeId)
		.andRequestTypeEqualTo(requestType);
		List<RequestAuthority> list=requestAuthorityDao.selectByExample(requestAuthorityExample);
		if (list.size()>0) {
			throw new ResultFaultException("0_00010");
		}
		
		RequestAuthority requestAuthority=new RequestAuthority();
		requestAuthority.setEmployeeId(employeeId);
		requestAuthority.setRequestType(requestType);
		requestAuthority.setHolidayQuota(requestAuthorityBean.getHolidayQuota());
		int record=requestAuthorityDao.insertSelective(requestAuthority);
		if (record>0) {
			return true;
		}
		return false;
	}

	/**
	 * 删除产假
	 */
	@Override
	public boolean deleteRequestAuthority(String requestAuthorityId) {
		int record=requestAuthorityDao.deleteByPrimaryKey(Integer.parseInt(requestAuthorityId));
		if (record>0) {
			return true;
		}
		return false;
	}

	/**
	 * 修改产假额度
	 */
	@Override
	public boolean updateRequestAuthority(RequestAuthorityBean requestAuthorityBean) {
		//更新验证updatetime
		int requestAuthorityId=requestAuthorityBean.getRequestAuthorityId();
		RequestAuthority requestAuthority=requestAuthorityDao.selectByPrimaryKey(requestAuthorityId);
		if (!requestAuthority.getUpdateTime().equals(requestAuthorityBean.getUpdateTime())) {
			throw new ResultFaultException("0_00011");
		}
		
//		RequestAuthority requestAuthority=new RequestAuthority();
//		requestAuthority.setRequestAuthorityId(requestAuthorityId);
//		requestAuthority.setEmployeeId(requestAuthorityBean.getEmployeeId());
//		requestAuthority.setRequestType(requestAuthorityBean.getRequestType());
		requestAuthority.setHolidayQuota(requestAuthorityBean.getHolidayQuota());
		int record=requestAuthorityDao.updateByPrimaryKeySelective(requestAuthority);
		if (record>0) {
			return true;
		}
		return false;
	}

	/**
	 * 查询产假信息
	 */
	@Override
	public RequestAuthorityPageBean getRequestAuthorityList(RequestAuthorityBean requestAuthorityBean) {
		RequestAuthorityPageBean requestAuthorityPageBean=new RequestAuthorityPageBean();
		requestAuthorityPageBean.setRequestAuthorityBeanList(requestAuthorityExtDao.getRequestAuthorityList(requestAuthorityBean));
		requestAuthorityPageBean.setPageTotal(requestAuthorityExtDao.count(requestAuthorityBean));
		return requestAuthorityPageBean;
	}

	/**
	 * 查询产假额度
	 */
	@Override
	public int getHolidayQuota(String regionType, String maternity) {
		return requestAuthorityExtDao.getHolidayQuota(Integer.parseInt(regionType), maternity);
	}

	/**
	 * 根据ID查询产假信息
	 */
	@Override
	public RequestAuthority getRequestAuthorityById(String requestAuthorityId) {
		return requestAuthorityDao.selectByPrimaryKey(Integer.parseInt(requestAuthorityId));
	}
	
	
}
